<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body">
            <h:form id="form" enctype="multipart/form-data">
                <br/>
                <h2 class="title">Pesquisando Memorial Descritivo</h2>
                <p:growl id="messages" showDetail="false" autoUpdate="true"/> 
                <br/>

                <p:panel id="pesquisaAvancadaPanel" styleClass="container_24 clearfix" header="Pesquisa Avançada" toggleable="true" collapsed="true">

                    <h4 class="title">Imóvel</h4>

                    <p:outputLabel value="Inclusão" for="tipoInclusao" styleClass="grid_3"/>
                    <p:selectBooleanCheckbox id="tipoInclusao" value="#{managerMemorialDescritivo.inclusao}" styleClass="grid_2">
                        <p:ajax update="@this, :form:tipoAlteracao" listener="#{managerMemorialDescritivo.atualizarTipoRegistro(true)}" />
                    </p:selectBooleanCheckbox>

                    <p:outputLabel value="Alteração" for="tipoAlteracao" styleClass="grid_3"/>
                    <p:selectBooleanCheckbox id="tipoAlteracao" value="#{managerMemorialDescritivo.alteracao}" styleClass="grid_2">
                        <p:ajax update="@this, :form:tipoInclusao" listener="#{managerMemorialDescritivo.atualizarTipoRegistro(false)}" />
                    </p:selectBooleanCheckbox>

                    <div class="clear"/>

                    <p:outputLabel value="Caracterização do Imóvel" styleClass="grid_3"/>
                    <div class="grid_6">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovel.caracterizacao}">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="EM_BRANCO" itemLabel="EM BRANCO"/>
                            <f:selectItems value="#{managerUtilitario.caracterizacao}"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Uso do Imóvel" styleClass="grid_4"/>
                    <div class="grid_6">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovel.usoImovel}">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItems value="#{managerUtilitario.usoImovel}"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <h4 class="title">Proprietário</h4>
                    <p:outputLabel value="Proprietário" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovelComProprietario}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Renda familiar" styleClass="grid_4" for="tempoMoradia"/>
                    <div class="grid_5">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.proprietario.rendaFamiliar}" panelStyle="width: 215px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItems value="#{managerUtilitario.rendaFamiliar}"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <p:outputLabel value="Tempo moradia" styleClass="grid_3" for="tempoMoradia"/>
                    <div class="grid_4">
                        <p:inputText id="tempoMoradia" value="#{managerMemorialDescritivo.proprietario.tempoMoradia}" style="width: 100%"/>
                    </div>

                    <p:outputLabel value="Tempo moradia municipio" styleClass="grid_4" for="tempoMoradiaMunicipio"/>
                    <p:inputText id="tempoMoradiaMunicipio" styleClass="grid_4" value="#{managerMemorialDescritivo.proprietario.tempoMoradiaMunicipio}"/>
                    <div class="clear"/>

                    <h4 class="title">Endereço</h4>
                    <p:outputLabel value="Número" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovelComNumero}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <h4 class="title">Anexos</h4>

                    <p:outputLabel value="Anexo" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovelComAnexo}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Croqui" styleClass="grid_4"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovelComCroqui}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear"/>

                    <h4 class="title">Dimensões e Confrontções</h4>

                    <p:outputLabel value="Dimensão" styleClass="grid_3"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovelComDimensoes}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                    <p:outputLabel value="Confrontação" styleClass="grid_4"/>
                    <div class="grid_4">
                        <p:selectOneMenu value="#{managerMemorialDescritivo.imovelComConfrontacao}" panelStyle="width: 150px">
                            <f:selectItem itemValue="#{null}" itemLabel=""/>
                            <f:selectItem itemValue="#{true}" itemLabel="Possui"/>
                            <f:selectItem itemValue="#{false}" itemLabel="Não possui"/>
                        </p:selectOneMenu>
                    </div>

                </p:panel>

                <p:panel id="fatorPanel" styleClass="container_24 clearfix withoutBorder">

                    <h4 class="title">Fator de Localização</h4>

                    <p:outputLabel value="Distrito" styleClass="grid_3" for="dInput" />
                    <p:inputText id="dInput" styleClass="grid_4" 
                                 value="#{managerMemorialDescritivo.imovel.inscricaoCadastralD}" />

                    <p:outputLabel value="Setor" styleClass="grid_3" for="sInput" />
                    <p:inputText id="sInput" styleClass="grid_4" 
                                 value="#{managerMemorialDescritivo.imovel.inscricaoCadastralS}" />

                    <p:outputLabel value="Quadra" styleClass="grid_3" for="qInput" />
                    <p:inputText id="qInput" styleClass="grid_4"
                                 value="#{managerMemorialDescritivo.inscricaoCadastralQuadra}"
                                 onkeydown="jQuery(this).validateMultipleNumber();"/>
                    <p:watermark value="1,2;3-5" for="qInput" rendered="true"/>
                    <div class="clear-both" />

                    <p:outputLabel value="Lote" styleClass="grid_3" for="lInput" />
                    <p:inputText id="lInput" styleClass="grid_4" 
                                 value="#{managerMemorialDescritivo.imovel.inscricaoCadastralLote}" />

                    <p:outputLabel value="Unidade" styleClass="grid_3" for="uInput" />
                    <p:inputText id="uInput" styleClass="grid_4" 
                                 value="#{managerMemorialDescritivo.imovel.inscricaoCadastralUnid}" />
                    <div class="clear-both" />
                </p:panel>
                <p:panel id="ownerPanel" styleClass="container_24 clearfix withoutBorder">

                    <h4 class="title">Proprietário</h4>

                    <p:outputLabel value="Proprietario: " styleClass="grid_3"/>
                    <p:selectOneRadio id="typePerson" 
                                      value="#{managerMemorialDescritivo.proprietario.tipoDocumento}" >  
                        <f:selectItems value="#{managerMemorialDescritivo.tipoDocumento()}" />
                        <p:ajax event="change" update=":form:ownerPanel" />
                    </p:selectOneRadio>

                    <div class="clear"/>

                    <p:outputLabel id="typeDocumentLabel" 
                                   value="#{managerMemorialDescritivo.proprietario.tipoDocumento}: " 
                                   styleClass="grid_3"/>

                    <p:inputMask id="typeDocumentInput" 
                                 value="#{managerMemorialDescritivo.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                          managerMemorialDescritivo.proprietario.cpf : managerMemorialDescritivo.proprietario.cnpj}"
                                 mask="#{managerMemorialDescritivo.proprietario.tipoDocumento.tipo eq 'CPF' ? 
                                         '999.999.999-99':'99.999.999/9999-99'}" 
                                 styleClass="grid_8"/>
                    <div class="clear"/>

                    <p:outputLabel value="Nome: " styleClass="grid_3" for="nomeInputText" 
                                   rendered="#{managerMemorialDescritivo.proprietario.tipoDocumento.tipo eq 'CPF'}"/>
                    <p:inputText id="nomeInputText" styleClass="grid_20 ajustInput" 
                                 value="#{managerMemorialDescritivo.proprietario.nome}" 
                                 rendered="#{managerMemorialDescritivo.proprietario.tipoDocumento.tipo eq 'CPF'}" />

                    <p:outputLabel value="Fantasia: " styleClass="grid_3" for="nameFantasyInputText" 
                                   rendered="#{managerMemorialDescritivo.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                    <p:inputText id="nameFantasyInputText" styleClass="grid_20 ajustInput"
                                 value="#{managerMemorialDescritivo.proprietario.nomeFantasia}" 
                                 rendered="#{managerMemorialDescritivo.proprietario.tipoDocumento.tipo eq 'CNPJ'}" />
                </p:panel>

                <p:panel id="addressPanel" styleClass="container_24 clearfix withoutBorder">

                    <h4 class="title">Endereço</h4>

                    <p:outputLabel value="CEP" styleClass="grid_3" for="cepInputMask" />
                    <p:inputMask id="cepInputMask" styleClass="grid_4" mask="99.999-999"
                                 value="#{managerMemorialDescritivo.endereco.cep}" />
                    <div class="clear-both" />

                    <p:outputLabel value="Número" styleClass="grid_3" for="numInputText" />
                    <p:inputText id="numInputText" styleClass="grid_2"
                                 value="#{managerMemorialDescritivo.endereco.numero}" />

                    <p:outputLabel value="Logradouro" styleClass="grid_4 alpha" for="patioInputText" />
                    <p:inputText id="patioInputText" styleClass="grid_14"
                                 value="#{managerMemorialDescritivo.endereco.logradouro}" />

                    <p:outputLabel value="Bairro" styleClass="grid_3" for="neighborhoodInputText" />
                    <p:inputText id="neighborhoodInputText" styleClass="grid_20"
                                 value="#{managerMemorialDescritivo.endereco.bairro}" />

                </p:panel>

                <br/>
                <div class="buttonAction">
                    <p:commandButton id="bt_add" value="Pesquisar" icon="ui-icon-search"
                                     update="tableImovel, @form"
                                     actionListener="#{managerMemorialDescritivo.pesquisarImovel()}"
                                     styleClass="ui-priority-primary" />
                    <p:commandButton id="bt_clear" value="Limpar" icon="ui-icon-close"
                                     actionListener="#{managerImovel.clear()}"
                                     update="@form" />

                    <p:commandButton  
                        title="Imprimir" icon="ui-icon-print"
                        update="@form" process="@this" disabled="#{managerMemorialDescritivo.imoveis eq null || managerMemorialDescritivo.imoveis.isEmpty()}"
                        actionListener="#{managerMemorialDescritivo.relatorioTodosMemorialDescritivo()}"
                        value="Imprimir"/>
                    <p:commandButton style="display: none;" widgetVar="imprimirTodosMemorialDescritivo"
                                     actionListener="#{managerMemorialDescritivo.imprimirTodosMemorialDescritivo()}"
                                     update="@form"
                                     ajax="false"
                                     onclick="this.form.target='_blank'"
                                     icon="ui-icon-check"/>
                </div >

                <p:blockUI id="BLOCK" block="resultPanel" trigger="bt_add" > 
                    <p:graphicImage value="media/images/ajax-loader.gif"/>
                </p:blockUI> 
                <p:blockUI id="BLOCK_CLEAR" block="resultPanel" trigger="bt_clear" > 
                    <p:graphicImage value="media/images/ajax-loader.gif"/>
                </p:blockUI>
                <h4 class="title">Resultado</h4>
                <p:panel id="resultPanel" styleClass="container_24 clearfix ">

                    <p:dataTable id="tableImovel" var="item" value="#{managerMemorialDescritivo.imoveis}" 
                                 styleClass="grid_24" rowIndexVar="rowid"
                                 rowKey="#{item.id}" rows="5" paginator="true"  
                                 paginatorPosition="bottom" emptyMessage="Nenhum registro">  
                        <f:facet name="header">
                            Imóvel #{managerMemorialDescritivo.imoveis.isEmpty() ? '' : "(".concat(managerMemorialDescritivo.imoveis.size()).concat(")")}
                        </f:facet>
                        <p:column headerText="Ação" width="30">
                            <p:commandButton disabled="#{item.proprietarios.isEmpty()}" 
                                             title="Imprimir" icon="ui-icon-print"
                                             update="@form"
                                             actionListener="#{managerMemorialDescritivo.relatorioMemorialDescritivo(item)}"/>
                            <p:commandButton style="display: none;" widgetVar="imprimirMemorialDescritivo"
                                             actionListener="#{managerMemorialDescritivo.imprimirMemorialDescritivo()}"
                                             update="@form"
                                             ajax="false"
                                             onclick="this.form.target='_blank'"
                                             icon="ui-icon-check"/>
                        </p:column>

                        <p:column headerText="Inscrição Cadastral" width="200" style="white-space: pre-wrap;">
                            <h:outputText value="#{managerPesquisarImovel.inscricaoCadastral(item)}" />
                        </p:column>
                        <p:column headerText="Localização" width="150">  
                            <ui:repeat value="#{item.enderecoImovel}" var="i">
                                <h:outputText value="Logradouro #{i.logradouro}, Bairro #{i.bairroDne ne null and i.bairroDne.nome ne null and !i.bairroDne.nome.isEmpty() ? i.bairroDne.nome : i.bairro} "/>
                            </ui:repeat>
                        </p:column>
                        <p:column headerText="Proprietário" width="100">  
                            <ui:repeat value="#{item.proprietarios}" var="o">
                                <h:outputText value="#{o.tipoDocumento eq 'CPF' ? o.nome : o.nomeFantasia}"/>
                                <br/>
                            </ui:repeat>
                            <p:outputLabel style="color: red" value="Não Existe Proprietário" rendered="#{item.proprietarios.isEmpty()}" />                    
                        </p:column> 

                    </p:dataTable> 

                </p:panel>

            </h:form>
        </ui:define>
    </ui:composition>

</html>
